package com.cci.kangdao.controller;

import com.cci.kangdao.aop.annotation.NoDuplicateSubmission;
import com.cci.kangdao.service.WorkOrderRepairService;
import com.cci.kangdao.service.impl.SMSEnum;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import com.cci.kangdao.utilTool.ParameterTool;
import com.cci.kangdao.utilTool.SerialNumberUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


@Controller
public class WorkOrderRepair {
	
	private Logger log = Logger.getLogger(this.getClass().getName());

	@Autowired
	private WorkOrderRepairService workOrderRepairService;
	
	@RequestMapping("/workOrderCreateId.aspx")
	public ModelAndView workOrderCreateId() {
		String regTime = LocalTimeUtil.getRealCurrentTime();
		String strDate = regTime.substring(0, 10).replace("-", "");
//		int i = (int)(Math.random()*900 + 100);
//		String myStr = Integer.toString(i);
//		String num = new SerialNumberUtil().getSerialNumber(ESN+strDate);
		
		String num =  SerialNumberUtil.getSerialNumber(strDate);
		System.out.println(num);
		return null;
	}
	
	/**
	 * 在线工单保修（工单生成）
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@NoDuplicateSubmission
	@RequestMapping("/workOrderCreate.aspx")
	public ModelAndView workOrderCreate(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			String eAppVersion = request.getHeader("eAppVersion");
			map.put("AppVersion",eAppVersion);
			//首先判断工单来源（0:用户APP报修，1：服务站APP报修，2：用户400报修，3：服务站CRM报修，4：CRM转派）
			//工单来源不同处理逻辑不同
			if(map.get("OrderSource")!=null) {
				String orderSource = map.get("OrderSource").toString();
				if ("0".equals(orderSource) || "5".equals(orderSource) || "6".equals(orderSource) || "7".equals(orderSource) || "8".equals(orderSource)) {
					//用户在线报修生成工单
					return workOrderRepairService.customerCreateWorkOrder(map);
				}else if("1".equals(orderSource)) {
					//服务站通过APP在线报修
					return workOrderRepairService.serviceStationCreateWorkOrder(map);
				}else if("2".equals(orderSource)) {
					//用户400报修
				}else if("3".equals(orderSource)) {
					//服务站CRM报修
				}else if("4".equals(orderSource)) {
					//CRM转派
				}else {
					String msg = "{\"status\": "+SMSEnum.type18.getIndex()+",\"msg\": \""+SMSEnum.type18.getName()+"\",\"data\":\"\"}";
					ParameterTool.writeResponse(response, msg);	
					return null;
				}
			}else {
				String msg = "{\"status\": "+SMSEnum.type17.getIndex()+",\"msg\": \""+SMSEnum.type17.getName()+"\",\"data\":\"\"}";
				ParameterTool.writeResponse(response, msg);	
				return null;
			}
			
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\",\"data\":\"\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}

    /**
     * sprint24     更新  gsn和下拉原因
     *
     * @param request
     * @return
     */
    @RequestMapping("updateGsnOrSymptomName.aspx")
    public ModelAndView updateGsnOrSymptomName(HttpServletRequest request, HttpServletResponse response) {
        return workOrderRepairService.updateGsnOrSymptomName(request, response);
    }

	
	/**
	 * 取消工单
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
    @NoDuplicateSubmission
    @RequestMapping("/workOrderCancel.aspx")
    public ModelAndView workOrderCancel(HttpServletRequest request,
                                        HttpServletResponse response) throws Exception{
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return workOrderRepairService.cancelWorkOrder(map);
		} catch (Exception ex) {
			// TODO: handle exception
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}

    /**
     *  校验服务工单是否超时 
     */
	@RequestMapping("/workOrderValidation.aspx")
	public void workOrderValidation(HttpServletRequest request, HttpServletResponse response){
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			workOrderRepairService.validationWorkOrder(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
	}

	@RequestMapping("/getWorkOrderOverTimeReason.aspx")
	public void getWorkOrderOverTimeReason(HttpServletRequest request, HttpServletResponse response){
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			workOrderRepairService.getWorkOrderOverTimeReason(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
	}

	
	
	/**
	 * 上传超时原因
	 */
	@RequestMapping("/workOrderOverTimeReason.aspx")
	public void workOrderOverTimeReason(HttpServletRequest request, HttpServletResponse response){
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			workOrderRepairService.workOrderOverTimeReason(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
	}

	/**
	 * 原因列表
	 * @param request
	 * @param response
	 */
	@RequestMapping("/workOrderReason.aspx")
	public void workOrderReason(HttpServletRequest request, HttpServletResponse response){
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			workOrderRepairService.workOrderReason(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
	}
	
}
